package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gxuwz.medical.database.DbUtil;
/**
 * 登录验证控制模块
 * @author HZS
 *
 */

public class LoginServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取用户名，密码
		String userid=req.getParameter("userid");
		String pwd=req.getParameter("pwd");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		req.setCharacterEncoding("UTF-8");
		PrintWriter out = resp.getWriter();
		ResultSet rs=null;
		PreparedStatement psmt =null;
		Connection conn=null;
		DbUtil dbUtil=new DbUtil();
		
		try{
			 if(userid.equals(userid)&&pwd.equals(pwd)){
		            conn=dbUtil.getConn();
		            String sql="select * from t_user where userid="+userid+" and pwd="+pwd+"";
		            psmt=conn.prepareStatement(sql);
		            rs=psmt.executeQuery();
		  
		          if(rs.next()){
		        	  resp.sendRedirect(req.getContextPath()+"/index.jsp");//登录成功跳转到index主页面
		          }else{
		        	  System.out.println("登录失败");
		        	  resp.sendRedirect(req.getContextPath()+"/login.jsp");
		          }
	}
			 }catch(Exception e){
		e.printStackTrace();
		
		
	}
		
		
		
	/*	if(userid.equals("123")&&pwd.equals("123")){
			 resp.sendRedirect(req.getContextPath()+"/index.jsp");//登录成功跳转到index主页面
        }else{
        	System.out.println("登录失败！");
           resp.sendRedirect(req.getContextPath()+"login.jsp");
        }*/
			
			
		}

	
		
	}

		
		
	